package cn.lingyangwl.agile.pay.common.service.impl;

import cn.lingyangwl.agile.pay.common.assembly.TradeDataAssembly;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import cn.lingyangwl.agile.pay.model.rqrs.pay.payorder.TradeDataQO;
import cn.lingyangwl.agile.pay.model.rqrs.pay.payorder.TradeDataRS;
import cn.lingyangwl.agile.pay.model.entity.TradeData;
import cn.lingyangwl.agile.pay.common.mapper.TradeDataMapper;
import cn.lingyangwl.agile.pay.common.service.TradeDataService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author shenguangyang
 */
@Service
public class TradeDataServiceImpl extends ServiceImpl<TradeDataMapper, TradeData> implements TradeDataService {
    @Resource
    private TradeDataAssembly tradeDataAssembly;

    @Override
    public TradeDataRS getTradeData(TradeDataQO req) {
        if (StringUtils.isAllEmpty(req.getMchOrderNo(), req.getPayOrderNo())) {
            throw new BizException("payOrderNo 和 mchOrderNo 不能同时为空");
        }

        LambdaQueryWrapper<TradeData> lqw = TradeData.lqw()
                .eq(TradeData::getMchNo, req.getMchNo())
                .eq(TradeData::getAppId, req.getAppId())
                .eq(StringUtils.isNotEmpty(req.getMchOrderNo()), TradeData::getMchOrderNo, req.getMchOrderNo())
                .eq(StringUtils.isNotEmpty(req.getPayOrderNo()), TradeData::getPayOrderNo, req.getPayOrderNo());
        return tradeDataAssembly.toTradeDataResp(this.getOne(lqw));
    }
}
